import warnings
warnings.warn("qturboframe.public_ui.ufc_wpf.context_menu模块已被废弃，替代模块为qturboframe.public_ui.ucf_wpf.context_menu", DeprecationWarning)

from datetime import datetime

from qturboframe.lib.ranorex.client import CsClient
from qturboframe.lib.ranorex.exceptions import CsClientError
from qturboframe.lib.utils.by import By
from qturboframe.public_ui.ufc_wpf.basecontrol import BaseControl
from qturboframe.public_ui.ufc_wpf.checkbox import CheckBox as UcfCheckBox
from qturboframe.public_ui.ufc_wpf.list_item import ListItem


class ContextMenu(BaseControl):
    """
    ucf wpf 上下文菜单

    :Attributes:
     - _driver: 控件所在的当前Control对象
     - _control: 控件元素Control对象
     - combobox: 当前的combobox对象
     - timeout: 步骤操作的超时时间

     :Raises
        CsClientError 没有找到控件异常

    :Example:
     - 例1: ContextMenu({By.XPATH:"/contextmenu"})
    """

    def __init__(self, element_para={By.XPATH:"/contextmenu"}, father=None, timeout=10000):
        cs = CsClient()
        xpath = element_para.get(By.XPATH)
        if cs.is_element_exist(xpath, time=timeout):
            self.context_menu = cs.find_control_by_path(element_para.get(By.XPATH), time=timeout)
        else:
            raise CsClientError("没有找到下拉菜单")
        self.timeout = timeout
        self._control = self.context_menu

    def unchecked_all_checkboxes(self):
        """
        wpf ucf uncheck所有已选中的checkbox

        :Example:
         - 例1: context_menu.unchecked_all_checkboxes('')
        """
        # 获取到所有的checkbox，并取消选中
        UcfCheckBox({By.XPATH: ".//checkbox"}, self.context_menu, timeout=self.timeout).unchecked_all_checkboxes()

    def single_select(self, value):
        """
        wpf ucf 组合框选择下拉内容 - 单选

        :Args:
         - value: 选择项的文本

        :Raise:
         - CsClientError 当选择的文字不在下拉框中时

        :Example:
         - 例1: context_menu.select('ghsfg 2')
        """
        if '-' in value:
            value_list = value.split("-")
        else:
            value_list = value.split(" ")
        ListItem({By.TEXT: value_list[0]}, self.context_menu, timeout=self.timeout).select_item(value_list)

    def multi_select(self, select_value, is_all=False):
        """
        wpf ucf 组合框选择下拉内容 - 多选

        :Args:
         - select_value: 选择项的文本， 多个文本时用逗号(,)分开
         - is_all: 是否全选

        :Raise:
         - CsClientError 当选择的文字不在下拉框中时

        :Example:
         - 例1: combobox.signle_select('ghsfg 2,sfhhtrt 3')
        """
        select_value_list = select_value.split(",")
        # 获取到所有的checkbox，并取消选中
        UcfCheckBox({By.XPATH: ".//checkbox"}, self.context_menu, timeout=self.timeout).unchecked_all_checkboxes()

        if is_all:
            UcfCheckBox({By.XPATH: ".//checkbox"}, self.context_menu, timeout=self.timeout).checked_all_checkboxes()
        else:
            for value in select_value_list:
                value_list = value.split(" ")
                ListItem({By.TEXT: value_list[0].strip()}, self.context_menu, timeout=self.timeout).select_item(value_list)


